package com.abc.cloud.service.impl;
import java.util.Date;

import cn.hutool.core.bean.BeanUtil;
import com.abc.cloud.dto.im.ChatResp;
import com.abc.cloud.entity.ChatDetailUserEntity;
import com.abc.cloud.entity.UserEntity;
import com.abc.cloud.service.ChatUserService;
import com.abc.cloud.service.UserService;
import com.abc.cloud.vo.im.AdminUpdateChatReq;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Query;

import com.abc.cloud.dao.ChatDao;
import com.abc.cloud.entity.ChatEntity;
import com.abc.cloud.service.ChatService;


@Service("chatService")
public class ChatServiceImpl extends ServiceImpl<ChatDao, ChatEntity> implements ChatService {

    @Autowired
    private ChatUserService chatUserService;

    @Autowired
    private UserService userService;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<ChatEntity> page = this.page(
                new Query<ChatEntity>().getPage(params),
                new QueryWrapper<ChatEntity>()
        );

        return new PageUtil(page);
    }

    @Override
    public List<ChatResp> listByUserId(Long userId) {
        List<ChatResp> chatRespList = this.baseMapper.queryListById(userId);
        for (ChatResp chatResp : chatRespList) {
            // 如果是单聊的话就查询成员列表
            if (chatResp.getType() == 1){
                chatResp.setUserList(chatUserService.listByChatId(chatResp.getId()));
            }
        }
        return chatRespList;
    }

    @Override
    public void updateLastMessage(Long chatId, String content) {
        ChatEntity chatEntity = this.baseMapper.selectById(chatId);
        chatEntity.setLastMessage(content);
        this.baseMapper.updateById(chatEntity);
    }

    @Override
    public void saveOneChat(Long userId, Long toUserId) {
        ChatEntity chatEntity = new ChatEntity();
        chatEntity.setName(null);
        chatEntity.setType(1);
        chatEntity.setIntroduce(null);
        chatEntity.setTotalCount(0);
        chatEntity.setUserId(userId);
        chatEntity.setIsShow(1);
        chatEntity.setCoverImage(null);
        chatEntity.setLastMessage(null);
        chatEntity.setCreateTime(new Date());
        chatEntity.setUpdateTime(new Date());
        chatEntity.setStatus(1);
        this.baseMapper.insertChat(chatEntity);

        // 保存聊天成员
        chatUserService.saveChatUser(chatEntity.getId(), userId);
        chatUserService.saveChatUser(chatEntity.getId(), toUserId);
    }

    @Override
    public PageUtil adminChatList(Map<String, Object> params) {
        QueryWrapper<ChatEntity> chatEntityQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (key != null && !key.isEmpty()){
            UserEntity userEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("username", key));
            if (userEntity == null){
                return new PageUtil(new ArrayList<>(), 0, 0, 0, 0);
            }
            chatEntityQueryWrapper.eq("user_id", userEntity.getId());
        }

        IPage<ChatEntity> page = this.page(
                new Query<ChatEntity>().getPage(params),
                chatEntityQueryWrapper
        );

        return new PageUtil(page);
    }

    @Override
    public void adminUpdateChat(AdminUpdateChatReq adminUpdateChatReq) {
        ChatEntity chatEntity = this.baseMapper.selectById(adminUpdateChatReq.getId());
        BeanUtils.copyProperties(adminUpdateChatReq, chatEntity);
        this.baseMapper.updateById(chatEntity);
    }

    @Override
    public void adminDeleteChat(List<Long> ids, Integer status) {
        this.baseMapper.deleteChatByIds(ids, status);
    }
}